package com.hc.server.actions;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

public class QUERY_TICKET implements IAction {

	private static final String CHECKIN_DATE = "CHECKIN_DATE";
	private final static String NAME = "NAME";
	private static final String SN = "SN";
	private final static String ADD = "ADD";
	private final static String PIC = "PIC";
	private final static String FEE = "FEE";// 费用包含
	private final static String KEY = "KEY";
	private final static String USAGE = "USAGE";// 如何使用
	private final static String NOTES = "NOTES";// 购买须知
	private final static String MPRICE = "MPRICE";
	private final static String SPRICE = "SPRICE";
	private final static String DES = "DES";
	private final static String STOCK = "STOCK";

	// MALL字段
	private final static String MALL_SN = "pdtSn";
	private final static String MALL_NAME = "name";
	private final static String MALL_ADDRESS = "address";
	private final static String MALL_PICURI = "picUri";
	// private final static String MALL_PHONE = "phone";
	private final static String MALL_DES = "brief";
	private final static String MALL_USAGE = "useExplain";
	private final static String MALL_FEE = "feeExplain";
	private final static String MALL_NOTES = "bookExplain";
	// private final static String MALL_PROMPT = "pdtExplain";
	private static final String MAll_MARKETPRICE = "marketPrice";
	private static final String MALL_SALEPRICE = "localPrice";
	private static final String MALL_STOCK = "stockCount";

	@Override
	public void execute(Request req, Response resp) {

		// 1.处理参数
		String pdtSn;
		try {
			pdtSn = req.getStrArg(SN);
		} catch (Exception e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD,
					Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		String checkInDate = req.optStrArg(CHECKIN_DATE);
		
		// 2.查询
		MallResponse mResp = null;
		try {
			mResp = MallServerHelper.queryTicket(pdtSn, checkInDate);
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		} catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		}
		if (0 != mResp.getRetCode()) {
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}

		// 3.组织返回数据
		try {
			JSONObject mTicket = mResp.getResult();
			String tPdtSn = mTicket.getString(MALL_SN);
			resp.appendRespContent(SN, tPdtSn);
			String tName = mTicket.getString(MALL_NAME);
			resp.appendRespContent(NAME, tName);
			String tAddress = mTicket.getString(MALL_ADDRESS);
			resp.appendRespContent(ADD, tAddress);
			String tPicUrl = mTicket.getString(MALL_PICURI);
			resp.appendRespContent(PIC, tPicUrl);
			resp.appendRespContent(KEY, "");
			String tFee = mTicket.getString(MALL_FEE);
			resp.appendRespContent(FEE, tFee);
			String tUsage = mTicket.getString(MALL_USAGE);
			resp.appendRespContent(USAGE, tUsage);
			String tNotes = mTicket.getString(MALL_NOTES);
			resp.appendRespContent(NOTES, tNotes);
			String tMarketPrice = mTicket.getString(MAll_MARKETPRICE);
			resp.appendRespContent(MPRICE, tMarketPrice);
			String tSalePrice = mTicket.getString(MALL_SALEPRICE);
			resp.appendRespContent(SPRICE, tSalePrice);
			String tDes = mTicket.getString(MALL_DES);
			resp.appendRespContent(DES, tDes);
			int stock = mTicket.getInt(MALL_STOCK);
			resp.appendRespContent(STOCK, stock);
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}
	}

}
